package uk.co.cocking.getinline2.matchers;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import uk.co.cocking.getinline2.record.Record;

public class FieldMatcher extends TypeSafeMatcher<Record> {
	
	private final String fieldName;
	private final Matcher<? super String> matcher;

	public FieldMatcher(String fieldName, Matcher<? super String> matcher) {
		this.fieldName = fieldName;
		this.matcher = matcher;
	}

	@Override
	public boolean matchesSafely(Record item) {
		if (item.hasField(fieldName) &&
				matcher.matches(item.getString(fieldName))) {
			return true;
		}
		return false;
	}

	@Override
	public void describeTo(Description description) {
		description.appendText(" a record with a field whose value is ");
	}

}
